package com.tgy.crud.config;

import com.tgy.crud.components.MyFilter;
import com.tgy.crud.components.MyServlet;
import com.tgy.crud.components.MyServletContextListener;
import com.tgy.crud.interceptor.PersonKeyGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletContextInitializerBeans;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Servlet;
import javax.sql.DataSource;
import java.util.Arrays;

@Slf4j
@Configuration
public class ServiceConfig {


    @Bean
    public ServletRegistrationBean servletRegistrationBean(){


        ServletRegistrationBean<Servlet> servletServletRegistrationBean = new ServletRegistrationBean<>();

        servletServletRegistrationBean.setServlet(new MyServlet());
        servletServletRegistrationBean.setUrlMappings(Arrays.asList("/hello"));
        return  servletServletRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean(){

        FilterRegistrationBean<MyFilter> myFilterFilterRegistrationBean = new FilterRegistrationBean<>(new MyFilter());
        myFilterFilterRegistrationBean.addUrlPatterns("/hello");

        return myFilterFilterRegistrationBean;
    }

    @Bean
    public ServletListenerRegistrationBean servletListenerRegistrationBean(){

        ServletListenerRegistrationBean<MyServletContextListener> registrationBean = new ServletListenerRegistrationBean<>(new MyServletContextListener());
        return registrationBean;
    }

    @Bean
    public PersonKeyGenerator personKeyGenerator(){

        return new PersonKeyGenerator();
    }

}
